package com.wwwarehouse.resource_center.adapter.defindeobjectstore;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ScreenUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.defindeobjectstore.DefinedObjectStoreBean;

import java.util.LinkedList;
import java.util.List;

/**
 * 定义实物资源的主页面的adapter。
 *
 */

public class DefinedObjectStoreAdapter extends RecyclerView.Adapter<DefinedObjectStoreAdapter.ViewHolder> {
    private List<DefinedObjectStoreBean.ListBean> lists = new LinkedList<>();
    private Context mContext;
    private View view;
    private int itemHeight;
    private DefinedObjectStoreAdapter.OnItemListener mOnItemListener;
    private void caculateItemHeight(){
        int screenHeight = ScreenUtils.getScreenHeight(mContext);
        int barHeight = ScreenUtils.getStatusHeight(mContext);
        itemHeight = (screenHeight - barHeight - ConvertUtils.dip2px(mContext,120)) / 5;
    }

    public DefinedObjectStoreAdapter(List<DefinedObjectStoreBean.ListBean> lists , Context mContext){
        this.lists = lists;
        this.mContext = mContext;
        caculateItemHeight();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.item_defined_object_store, parent, false);
        ViewHolder holder = new ViewHolder(view);

        RecyclerView.LayoutParams params =  new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.height = itemHeight;
        view.setLayoutParams(params);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        DefinedObjectStoreBean.ListBean listBean = lists.get(position);
        if ("1".equals(listBean.getDefaultInventory())) {
            holder.inRel.setBackground(mContext.getResources().getDrawable(R.drawable.selector_defined_object_main_new));
        }
        holder.titleTxt.setText(listBean.getName());
        if (mOnItemListener != null) {
            holder.inRel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemListener.onItem(holder.inRel,position);
                }
            });
        }
    }


    @Override
    public int getItemCount() {
        return lists.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public RelativeLayout mainRel,inRel;
        public TextView titleTxt;

        public ViewHolder(View itemView) {
            super(itemView);
            mainRel = (RelativeLayout) itemView.findViewById(R.id.defined_object_recycleview_item_rel);
            inRel = (RelativeLayout)itemView.findViewById(R.id.defined_object_recycleview_item_in_rel);
            titleTxt = (TextView)itemView.findViewById(R.id.defined_object_recycleview_item_text);
        }
    }

    public interface OnItemListener{
        void onItem(RelativeLayout inRel,int position);
    }

    public void setOnItemListener(DefinedObjectStoreAdapter.OnItemListener mOnItemListener) {
        this.mOnItemListener = mOnItemListener;
    }
}
